package com.cn.aam.checaiduo.ui.me.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.base.web.RedToolbarWebActivity;
import com.cn.aam.checaiduo.bean.BannerB;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.me.BeginnerParkActivity;
import com.cn.aam.checaiduo.ui.me.PlatformIntroduceActivity;
import com.cn.aam.checaiduo.utils.CalendarUtil;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.utils.WindowsUtils;

import butterknife.BindView;

/**
 * monker on 2017/6/28.
 * 平台介绍和活动中心共用
 */

public class PlatformIntroduceAdapter extends BaseRecycerAdapter<BannerB> {
    private String flag = PlatformIntroduceActivity.FLAG_INTRODUCE;//默认平台介绍

    public PlatformIntroduceAdapter(Context mContext) {
        super(mContext);
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
        return new PlatformIntroduceHolder(picNewsView);
    }

    @Override
    protected int getItemLayoutRes() {
        return R.layout.banner_item_home;
    }

    @Override
    protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {

        PlatformIntroduceHolder holder = (PlatformIntroduceHolder) recyclerholder;

        holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(mContext).load(getItem(position).getBanner()).into(holder.imageView);
        if (TextUtils.equals(flag, PlatformIntroduceActivity.FLAG_ACTIVITY)) {
            holder.llActivity.setVisibility(View.VISIBLE);
            holder.tvTitle.setText(getItem(position).getTitle());
            holder.tvDate.setText(CalendarUtil.getDate5(getItem(position).getCreated_time()));
        } else {
            holder.llActivity.setVisibility(View.GONE);
        }

        if (position==getDataList().size()-1){
           holder.itemView.setPadding(0 ,WindowsUtils.dip2px(10),0,WindowsUtils.dip2px(10));
        }else{
            holder.itemView.setPadding(0 ,WindowsUtils.dip2px(10),0,0);
        }

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyViewsUtils.isFastDoubleClick()) return;
                BannerB item = getItem(position);

                if (TextUtils.equals(flag, PlatformIntroduceActivity.FLAG_ACTIVITY)) {
                    if (!TextUtils.isEmpty(item.getLink())) {
                        if (TextUtils.equals("BeginnerParkActivity", item.getLink())) {
                            mContext.startActivity(BeginnerParkActivity.newInstance(mContext));
                        } else if (TextUtils.equals("InvitePrizesActivity", item.getLink())) {
                            UIRouter.getInstance().openPage(mContext, UIRouter.ME.AC_INVITEPRIZES);
                        } else {
                            mContext.startActivity(RedToolbarWebActivity.newInstance(mContext, item.getLink(), item.getTitle()));
                        }
                    } else if (!TextUtils.isEmpty(item.getUrl())) {
                        mContext.startActivity(RedToolbarWebActivity.newInstance(mContext, item.getUrl(), item.getTitle()));
                    }
                } else {
                    String url = item.getLink();
                    if (url == null) return;
                    mContext.startActivity(RedToolbarWebActivity.newInstance(mContext, url, getItem(position).getTitle()));
                }
            }
        });


    }

    class PlatformIntroduceHolder extends BaseRecyclerViewHolder {
        @BindView(R.id.imageView)
        ImageView imageView;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.ll_activity)
        LinearLayout llActivity;


        public PlatformIntroduceHolder(View itemView) {
            super(itemView);
            int hight = WindowsUtils.getScreenWidth(mContext) * 272 / 750;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, hight);
            imageView.setLayoutParams(params);
        }
    }

}
